package com.example.demo.controller;


import com.example.demo.entity.Image;
import com.example.demo.service.Impl.ReptileSelectImpl;
import com.example.demo.service.Impl.SelectImpl;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//图片操作的接口
@RestController
@RequestMapping("/Image")
public class SelectController {

//    图片操作
    SelectImpl selectimpl=new SelectImpl();

    @ResponseBody
    @RequestMapping("/select")
    public List<Image> select(){
        List<Image> list=new ArrayList<>();
        list=selectimpl.select();
        System.out.println(list);
        return list;
    }

//    图片爬虫
    @RequestMapping("/reptile")
    public Boolean reptile(String https){
        ReptileSelectImpl reptileselectimpl=new ReptileSelectImpl();
        try{
            reptileselectimpl.Reptile(https);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

//    图片搜索
    @RequestMapping("/vagueImageSelect")
    public List<Image> vagueImageSelect(String title){
        List<Image> list=new ArrayList<>();
        if(title.length()!=0){
            try{
                list=selectimpl.vagueImageSelect(title);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return list;
    }


//    图片添加(本地上传)

    @RequestMapping("/addImage")
    public boolean addImage(@RequestParam("file") MultipartFile file,
                         @RequestParam("file") MultipartFile originalfile,
                         @RequestParam Long type,
                         @RequestParam String title,
                         HttpServletRequest request) throws Exception {
//        上传时的文件名
        String fileName=file.getOriginalFilename();
        String originalfileName=originalfile.getOriginalFilename();

        //文件后缀名
        String fileName1=fileName.substring(fileName.length()-3,fileName.length());
        String originalfileName1 =originalfileName.substring(originalfileName.length(),originalfileName.length()-3);
        if((fileName1=="jpg"||fileName1=="bmp"||fileName1=="png")&&(originalfileName1=="jpg"||originalfileName1=="bmp"||originalfileName1=="png")){
            if(selectimpl.addlocalImage(file,originalfile,type,title)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }
//    图片添加（链接添加）
    @RequestMapping("/addImagehttp")
    public boolean addImagehttp(@RequestParam String Image,
                             @RequestParam String originalfile,
                             @RequestParam Long type,
                             @RequestParam String title){
        if(selectimpl.addhttpImage(Image,originalfile,type,title)){
            return true;
        }else{
            return false;
        }

    }

//    图片删除
    @RequestMapping("/deleteImage")
    public boolean deleteImage(@RequestParam String addrImage,//压缩图地址
                            @RequestParam String addroriginalfile){//原图地址
        if(addrImage.substring(0,9).equals("C:\\Image")&&addroriginalfile.substring(0,17).equals("C:\\originalImage")){
            if(selectimpl.delectlocalImage(addrImage,addroriginalfile)){
                return true;
            }else{
                return false;
            }
        }else{
            if(selectimpl.delecthttpImage(addrImage,addroriginalfile)){
                return true;
            }else{
                return false;
            }
        }
    }

//    图片更改
    @RequestMapping("/updateImage")
    @ResponseBody
    public boolean updeleteImage( Image newImage,
                               Image oldImage){
        if(newImage!=null&oldImage!=null){
            if(selectimpl.updataImage(newImage, oldImage)){
                return true;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }

//    图片下载

    @RequestMapping("/downloadImage")
    public boolean downloadImage(String urlString, String filename){

        if(urlString.length()!=0&&filename.length()!=0){
            if(selectimpl.downloadImage(urlString, filename)){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

}
